package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.ActivityService;
import com.youi.service.ActivitygoodsService;
import com.youi.entity.Activity;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 活动信息控制层
* @author Administrator
*/
@Controller
public class ActivityController  extends HttpRequestManage{

		@Autowired
		private ActivityService activityService;
		@Autowired
		private ActivitygoodsService activitygoodsService;

		/**
		 * I-47
		 * 添加或编辑活动信息
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/addOrEditActivity")
		public String addOrEditActivity(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Activity activity=null;
			if (id!=null) {
				activity=activityService.get(id);
			}else {
				activity=new Activity();
				activity.setDelStatus(DelStatus.NODEL);
				activity.setGmtCreated(new Date());
			}
			//状态(0启用 1关闭)
			Integer status=optInteger("status",request);
			if (status!=null){
				activity.setStatus(status);
			}
			//活动名称
			String name=optString("name",request);
			if (name!=null){
				activity.setName(name);
			}
			Long storeGroupId=optLong("storeGroupId",request);
			if (storeGroupId!=null){
				activity.setStoreGroupId(storeGroupId);
			}
			//展示开始时间
			Date showStart=optDate("showStart",request,"yyyy-MM-dd HH:mm:ss");
			if (showStart!=null){
				activity.setShowStart(showStart);
			}
			//展示结束时间
			Date showEnd=optDate("showEnd",request,"yyyy-MM-dd HH:mm:ss");
			if (showEnd!=null){
				activity.setShowEnd(showEnd);
			}
			//每人限购数
			Integer perLimitCnt=optInteger("perLimitCnt",request);
			if (perLimitCnt!=null){
				activity.setPerLimitCnt(perLimitCnt);
			}
			//每人每天限购数
			Integer groupNum=optInteger("groupNum",request);
			if (groupNum!=null){
				activity.setGroupNum(groupNum);
			}
			//门店返润百分比
			Double percent=optDouble("percent",request);
			if (percent!=null){
				activity.setPercent(percent);
			}
			//活动介绍
			String activityInfo=optString("activityInfo",request);
			if (activityInfo!=null){
				activity.setActivityInfo(activityInfo);
			}
			String img=optString("img",request);
			if (img!=null){
				activity.setImg(img);
			}
			//备注
			String remark=optString("remark",request);
			if (remark!=null){
				activity.setRemark(remark);
			}
			//最高原价
			Double maxOriginPrice=optDouble("maxOriginPrice",request);
			if (maxOriginPrice!=null){
				activity.setMaxOriginPrice(maxOriginPrice);
			}
			//最低原价
			Double minOriginPrice=optDouble("minOriginPrice",request);
			if (minOriginPrice!=null){
				activity.setMinOriginPrice(minOriginPrice);
			}
			String companyCode=optString("companyCode",request);
			if (companyCode!=null){
				activity.setCompanyCode(companyCode);
			}
			Double sponsorPercent=optDouble("sponsorPercent",request);
			if (sponsorPercent!=null){
				activity.setSponsorPercent(sponsorPercent);
			}
			Integer isPickUpStore=optInteger("isPickUpStore",request);
			if (isPickUpStore!=null){
				activity.setIsPickUpStore(isPickUpStore);
			}
			String payStoreCode=optString("payStoreCode",request);
			if (payStoreCode!=null){
				activity.setPayStoreCode(payStoreCode);
			}
			//作弊访问数量
			Integer cheatVisitNum=optInteger("cheatVisitNum",request);
			if (cheatVisitNum!=null){
				activity.setCheatVisitNum(cheatVisitNum);
			}
			Integer realVisitNum=optInteger("realVisitNum",request);
			if (realVisitNum!=null){
				activity.setRealVisitNum(realVisitNum);
			}
			
			String bigImg=optString("bigImg",request);
			if (bigImg!=null){
				activity.setBigImg(bigImg);
			}
			//虚拟作弊已售数
			Integer cheatSaleCnt=optInteger("cheatSaleCnt",request);
			if (cheatSaleCnt!=null){
				activity.setCheatSaleCnt(cheatSaleCnt);
			}
			Integer realSlaeNumCnt=optInteger("realSlaeNumCnt",request);
			if (realSlaeNumCnt!=null){
				activity.setRealSlaeNumCnt(realSlaeNumCnt);
			}
			//虚拟库存数
			Integer cheatStockCnt=optInteger("cheatStockCnt",request);
			if (cheatStockCnt!=null){
				activity.setCheatStockCnt(cheatStockCnt);
			}
			//真实库存数
			Integer realStockCnt=optInteger("realStockCnt",request);
			if (realStockCnt!=null){
				activity.setRealStockCnt(realStockCnt);
			}
			//活动开始时间
			Date gmtStart=optDate("gmtStart",request,"yyyy-MM-dd HH:mm:ss");
			if (gmtStart!=null){
				activity.setGmtStart(gmtStart);
			}
			//活动结束时间
			Date gmtEnd=optDate("gmtEnd",request,"yyyy-MM-dd HH:mm:ss");
			if (gmtEnd!=null){
				activity.setGmtEnd(gmtEnd);
			}
			activity.setGmtModify(new Date());
			activityService.saveOrUpdate(activity);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-48
		 * 删除活动信息
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/delActivityByIds")
		public String delActivityByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Activity> activityList= activityService.getByParams(params);
			if (activityList==null||activityList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Activity activity : activityList) {
				activity.setDelStatus(DelStatus.DEL);
				activityService.delActivityGoodsByActId(activity);
			}
			activityService.saveOrUpdateAll(activityList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-45
		 * 查询活动信息列表
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getActivity")
		public String getActivity(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Activity> activityList=activityService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (activityList==null||activityList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=activityService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, activityList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-46
		 * 查询活动信息详情
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getActivityById")
		public String getActivityById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Activity activity=activityService.get(id);
			if (activity==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",activity);//jsonObject	json对象
		}

		/**
		 * I-52
		 * 查询所有活动信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getAllActivity")
		public String getAllActivity(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			List<Activity> activityList= activityService.getByParams(prams);
			if (activityList==null||activityList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",activityList);//jsonObjectList	json对象集合
		}
}
